import { defineStore } from "pinia";
import {
  getTableData,
  createTableData,
  removeTableData,
  editeTableData
} from "@/services/table";
import {ListName} from './type'

const useTableStore = defineStore('table', {
  state: () => ({
    usersList: [],
    usersCount: 0,
    departmentList: [],
    departmentCount: 0,
    roleList: [],
    roleCount: 0,
    menuList: [],
    menuCount: 0,
    categoryList: [],
    categoryCount: 0,
    goodsList: [],
    goodsCount: 0,
    storyList: [],
    storyCount:0
  }),
  actions: {
    async getTableAction(listName: string, data: any) {
      const res = await getTableData(listName, data)
      console.log(res);
      
      this.$patch({
        [`${listName}List`]: res.data.list,
        [`${listName}Count`]:res.data.totalCount
      })
    },
    async createTableAction(listName: string, data: any) {
      const res = await createTableData(listName, data)
      this.getTableAction(listName,{})
    },
    async removeTableAction(listName: string, id: number) {
      const res = await removeTableData(listName,id)
      this.getTableAction(listName,{})
    },
    async editeTableAction(listName: string, id: number, data: any) {      
      const res = await editeTableData(listName, id, data)
      this.getTableAction(listName,{})
    }
  },
  getters: {
    dataList(state) {
      return (listName: ListName) => state[`${listName}List`]
    },
    dataCount(state) {
      return (listName: ListName) => state[`${listName}Count`]
    }
  }
})

export default useTableStore